
#include <stdio.h>
#include "mem_manage.h"
#include "writefortest.h"
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>

#define DEBUG
typedef unsigned char u_char;
typedef unsigned int u_int;
typedef unsigned short int s_int;


typedef struct MailInfo
{
	unsigned int sorip;
	unsigned short sorport;
	unsigned int desip;
	unsigned int desport;
	unsigned int mailsize;
	unsigned int mailres;
}mailinfo;

int main()
{
	int mailcount;
	int readsize = 0;
	char * mailtmpsave = NULL;
	int semid;
	void * smptr;
	u_int mailsize = 0;
	u_char * head;
	char * mailsubject = NULL;
	char * mailtext = NULL;
	int rest = -1;
	u_int * uiptr = NULL;
	s_int * siptr = NULL;
	int writesize = 0;
	int sublen = 0;
	int textlen = 0;
	char * subptr = NULL;
	char * textptr = NULL;
    char * testptr = NULL;
    mailinfo * mailhead = NULL;

	//open sem
    semid=getSem(S_SEM_NAME,S_SEM_ID,GET);               //共享内存id号
	mem_start=getMem(S_MEM_NAME,S_MEM_ID,TOTAL_LEN,GET); //打开共享内存
	smptr=mem_start;                                     //共享内存链接

	printf("Read for read sub and text\n");
	mailhead = (mailinfo *)malloc(sizeof(mailinfo));
	assert(mailhead != NULL);
	//memset(mailhead,0,sizeof(mailinfo));

	for(mailcount = 1;;++mailcount)
	{
		for(;;) //读第一次
        {
            memset(mailhead,0,sizeof(mailinfo));
            readsize = my_read(&smptr,mailhead,semid);  //读取首部信息
            //循环等待读取首部信息
            if(readsize == READ_FAILED)
            {
#ifdef DEBUG
                printf("read wait~\n");
#endif
                sleep(1);
            }
            else if(readsize == 0)
            {
#ifdef DEBUG
                printf("read 0 char~\n");
#endif
                sleep(1);
            }
            else
            {
                printf("\n----------------Mail head ----------------\n");
                printf("Readsize: %d\n",readsize);
                printf("sortip %u| desip %u\n | sorport %u | desport %u\n",mailhead->sorip,mailhead->desip,mailhead->sorport,mailhead->desport);
                printf("maisize is %u | res is %u\n",mailhead->mailsize, mailhead->mailres);
                mailsize =(int)mailhead->mailsize;
                printf("\n----------------Mail head ---------------------------\n");
	            printf("mailsize is %u\n",mailsize);
	            break;
            }
        }

        for(; ;)    //读第二次
        {


            mailtmpsave = NULL;
            mailtmpsave = (char *)malloc((mailsize) * sizeof(char)+1);
            assert(mailtmpsave!=NULL);
            memset(mailtmpsave,0,mailsize);
            readsize = my_read(&smptr,mailtmpsave,semid);
            
            testptr = (char*)malloc( 100 * sizeof( char )+1);
            testptr[100] = '\0';
            free( testptr );
          
            if(readsize == READ_FAILED)
            {
                printf("read mail fail then sleep\n");
                free(mailtmpsave);
                sleep(1);
            }
            else if(readsize == 0)
            {
                printf("read 0 char of mailcontent~\n");
                free(mailtmpsave);
                sleep(1);
            }
            else  
            {
                printf(" %d\n", readsize );
                siptr = (s_int *)mailtmpsave;
                sublen = (int)*siptr;
                printf("sublen is %d\n",sublen);
                siptr = (s_int *)(mailtmpsave+2);
                textlen = (int)*siptr;
                printf("textlen is %d\n",textlen);    
                
                subptr = (char *)malloc( sizeof(char)*sublen);
                assert(subptr != NULL);
                strncpy(subptr,mailtmpsave+4,sublen-1);
                subptr[sublen-1] = '\0';
                printf("subject is %s\n", subptr);

                textptr = (char *)malloc( sizeof(char)*textlen);
                assert(textptr != NULL);
                memcpy(textptr,mailtmpsave+4+sublen, textlen-1);
                textptr[textlen-1] = '\0';
                printf("text is %s\n", textptr);

                write_sharemem(mailtmpsave+4,readsize-4,mailcount);

                free(subptr);
                free(textptr);
                free(mailtmpsave);
                subptr = NULL;
                textptr = NULL;
                mailtmpsave = NULL;

                printf("free all ok!\n");
                break;
            } //end else
        }   //end for

        printf("the %d mailtext is OK\n",mailcount);
    } // end for
    free(mailhead);
}


